package sample;

import javafx.application.Application;
import javafx.embed.swing.SwingFXUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Rectangle2D;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.ScrollPane;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;


import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class BeautyMain extends Application {
    private FirstAnchorPane firstAnchorPane = new FirstAnchorPane();
    private SecondAnchorPane secondAnchorPane = new SecondAnchorPane();
    private ThirdAnchorPane thirdAnchorPane = new ThirdAnchorPane();
    private FourthAnchorPane fourthAnchorPane = new FourthAnchorPane();
    VBox vB1;
    VBox vB2;
    VBox vB3;
    VBox vB4;

    public int num = 1;


    public static String themeURL = "";

    public static void main(String [] args){
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {

        VBox root = new VBox();
        Scene scene = new Scene(root,1000,900);
        VBox vBox = new VBox();
        HBox buttons = new HBox();
        ScrollPane sc = new ScrollPane();
        SetSize.SetElementSize(sc,1000,854);
        VBox content = ViewVBox();
        Button cutTheme = new Button("切换主题");
        cutTheme.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {

                DirectoryChooser directoryChooser = new DirectoryChooser();
                directoryChooser.setTitle("选择主题");
                File chosenDir = directoryChooser.showDialog(primaryStage);
                if (chosenDir != null) {

                    String url = chosenDir.getAbsolutePath();
                    themeURL = url;

                    try{
                        sc.setContent(ViewVBox());
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                } else {
                    System.out.print("no directory chosen");
                }



            }
        });
        Button createPreview = new Button("生成预览");
        createPreview.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event) {
                try{
                    //AnchorPane anchorPane = firstAnchorPane.firstAnchorPane();

                    snapshot(sc.getContent(),"preview_settings_0",10,0);
                    snapshot(sc.getContent(),"preview_contact_0",510,0);
                    snapshot(sc.getContent(),"preview_contact_1",1010,0);
                    snapshot(sc.getContent(),"preview_launcher_0",1510,0);
                    openFile(new File(themeURL));
                }catch (Exception e){
                    e.printStackTrace();
                }


            }
        });


        Button pack = new Button("打包");
        pack.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                try{
                    File f = new File(themeURL);
                    new CompactAlgorithm(new File("D:", f.getName() + ".itz")).zipFiles(f);
                    openFile(new File("D:"));
                }catch (Exception e){
                    e.printStackTrace();
                }

            }
        });

//        Button sin = new Button("单个预览");
//        sin.setOnAction(new EventHandler<ActionEvent>() {
//            @Override
//
//            public void handle(ActionEvent event){
//                vB1.setPadding(new Insets(0,0,0,100));
//                vB2.setPadding(new Insets(0,0,0,100));
//                vB3.setPadding(new Insets(0,0,0,100));
//                vB4.setPadding(new Insets(0,0,0,100));
//                try{
//                    if(num==1){
//                        sc.setContent(vB1);
//                        num++;
//                    }else if(num==2){
//                        sc.setContent(vB2);
//                        num++;
//                    }else if(num==3){
//                        sc.setContent(vB3);
//                        num++;
//                    }else if(num==4){
//                        sc.setContent(vB4);
//                        num++;
//
//                    }else if(num==5){
//
//                            sc.setContent(ViewVBox());
//
//                        num=1;
//                    }
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
//
//
//            }
//        });


        buttons.getChildren().addAll(cutTheme,createPreview,pack);
        if(BeautyMain.themeURL==""||BeautyMain.themeURL==null){

//              root.getChildren().add(vBox1);
            sc.setContent(new VBox());
        }else{
            sc.setContent(content);
        }
        //vBox.getChildren().addAll();
        vBox.getChildren().addAll(buttons,sc);


        root.getChildren().addAll(vBox);

        buttons.setMargin(cutTheme,new Insets(10,20,20,20));
        buttons.setMargin(createPreview,new Insets(10,20,20,20));
        buttons.setMargin(pack,new Insets(10,20,20,20));
        //buttons.setMargin(sin,new Insets(10,20,20,20));
        primaryStage.setScene(scene);
        primaryStage.setTitle("主题预览");
        //primaryStage.setResizable(false);
        primaryStage.show();
    }

    public VBox ViewVBox()throws Exception{
        if(BeautyMain.themeURL==null||BeautyMain.themeURL==""){
            return new VBox();
        }
        VBox vBox = new VBox();
//        vBox.setPrefWidth(2200);
        vBox.setMaxWidth(2000);
        vBox.setPrefWidth(2000);
        vBox.setMinWidth(2000);
        HBox hBox = new HBox();
//        hBox.setPrefWidth(2200);
        hBox.setMaxWidth(2000);
        hBox.setMinWidth(2000);
        //左一
        VBox pane1 = new VBox();
        AnchorPane anchorPane1 = new AnchorPane();
        pane1.getChildren().addAll(anchorPane1);
//        ancho = firstAnchorPane.firstAnchorPane();
        anchorPane1.getChildren().addAll(firstAnchorPane.firstAnchorPane());
//        pane1.setPrefWidth(480);
//        pane1.setMinWidth(480);
//        pane1.setMaxWidth(480);
//        pane1.setPrefHeight(854);
        SetSize.SetElementSize(pane1,480,854);

        File file = new File(BeautyMain.themeURL+"\\com.android.dialer\\contact_editor_delete_button_bg.9.png");
        ImageView imageView = new ImageView();
        String locaImage = file.toURI().toURL().toString();
        Image image = new Image(locaImage, true);
        imageView.setImage(image);

        //左二
        VBox pane2 = new VBox();
        AnchorPane anchorPane2 = new AnchorPane();
        pane2.getChildren().addAll(anchorPane2);
        anchorPane2.getChildren().addAll(secondAnchorPane.SecondAnchorPane());
//        pane2.setPrefWidth(480);
//        pane2.setMinWidth(480);
//        pane2.setMaxWidth(480);
//        pane2.setPrefHeight(854);
        SetSize.SetElementSize(pane2,480,854);
        //右二
        VBox pane3 = new VBox();
        AnchorPane anchorPane3 = new AnchorPane();
        pane3.getChildren().addAll(anchorPane3);
        anchorPane3.getChildren().addAll(thirdAnchorPane.ThirdAnchorPane());
        pane3.setPrefWidth(480);
        pane3.setMinWidth(480);
        pane3.setMaxWidth(480);
        pane3.setPrefHeight(854);
        SetSize.SetElementSize(pane3,480,854);
        //右一
        VBox pane4 = new VBox();
        AnchorPane anchorPane4 = new AnchorPane();
        pane4.getChildren().addAll(anchorPane4);
        anchorPane4.getChildren().addAll(fourthAnchorPane.FourthAnchorPane());
//        pane4.setPrefWidth(480);
//        pane4.setMinWidth(480);
//        pane4.setMaxWidth(480);
        SetSize.SetElementSize(pane4,480,854);



        //SetSize.SetElementSize(firstVBox,480,854);
        vB1=pane1;
        vB2=pane2;
        vB3=pane3;
        vB4=pane4;

        HBox.setMargin(pane1,new Insets(0,10,0,10));
        HBox.setMargin(pane2,new Insets(0,10,0,10));
        HBox.setMargin(pane3,new Insets(0,10,0,10));
        HBox.setMargin(pane4,new Insets(0,10,0,10));
        hBox.getChildren().addAll(pane1,pane2,pane3,pane4);
        vBox.getChildren().addAll(hBox);
        return vBox;
    }



    public void snapshot(Node node,String name,int minX,int minY) {
        //以下两句是设置截图的参数，具体细节还没有研究
        final SnapshotParameters params = new SnapshotParameters();
        params.setFill(Color.TRANSPARENT);
        params.setViewport(new Rectangle2D(minX,minY,480,854));
        //对Node进行截图，只会截取显示出来的部分，未显示出来的部分无法截图（没有火狐截图高级）

//        Image image = node.snapshot(params, null);
//        try {
//            ImageIO.write(SwingFXUtils.fromFXImage(image, null), "jpg",
//                    new File("C:\\Users\\Administrator\\Desktop\\桌面\\"+name+".jpg"));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        WritableImage snapshot = node.snapshot(params, null);
        //将JavaFX格式的WritableImage对象转换成AWT BufferedImage 对象来进行保存
        final BufferedImage image
                = SwingFXUtils.fromFXImage(snapshot, null);
        //通过ImageIO来存储图片
        final File file = new File(BeautyMain.themeURL+"\\preview\\"+name+".jpg");

        try {
            ImageIO.write(image, "png", file);
        } catch (java.io.IOException e) {
        }
    }

    public static void openFile(File file) {
        EventQueue.invokeLater(() -> {
            try {
                Desktop.getDesktop().open(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

}
